#!/usr/bin/env node

const fs = require('fs');
const path = require('path');
const execSync = require('child_process').execSync;
try {
  const result = execSync('git status -s', { encoding: 'utf-8' });
  const forbidRegistry = require("../../package.json").forbidRegistry || [
    'registry.npmjs.org',
    'registry.yarnpkg.com',
  ];
  const fileList = result.split('\n').filter((item) => {
    return /^[M|A]/.test(item);
  }).map((item) => {
    if (/^[M|A]  '(.+?)'$/.test(item)) {
      item = item.replace(/^[M|A]  '(.+?)'$/, '$1');
    } else {
      item = item.replace(/^[M|A]  (.+?)$/, '$1');
    }
    return path.resolve(item);
  });
  for (const file of fileList) {
    if (file.includes('yarn.lock') || file.includes('package-lock.json')) {
      const str = fs.readFileSync(file).toString();
      for (const item of forbidRegistry) {
        if (str.includes(item)) {
          console.error(`${ file }包含 ${ item }!`);
          process.exit(1);
        }
      }
    }
  }
} catch (error) {
  console.error(error);
  process.exit(1);
}

process.exit(0);